package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.rebbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
public class DeadLetterMessage {
    //基于死信的延迟消息
    @RabbitListener(queues = DeadLetterMqConfig.queue_dead_2)
    public void message(String message, Message messages, Channel channel){
        try {
            System.out.println("延迟队列接收的消息是："+message);
            System.out.println("接收时间为："+new Date());
            channel.basicAck(messages.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
           // e.printStackTrace();
            if(!messages.getMessageProperties().isRedelivered()){
                try {
                    channel.basicNack(messages.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                try {
                    channel.basicReject(messages.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
